﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using FSLib.Network.Http;
using Newtonsoft.Json;

namespace DownCode
{
    public static class MainClass
    {
        /// <summary>
        /// Manifest转List
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static List<Models> StringToList(string str)
        {
            var arg = JsonConvert.DeserializeObject<dynamic>(str);

            var list = new List<Models>();
            foreach (var dy in arg.assets)
            {
                var mod = new Models
                {
                    Name = dy.Name,
                    Path = dy.Name,
                    Md5 = dy.Value.md5,
                    Size = dy.Value.size
                };
                list.Add(mod);
            }
            return list;
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        private static string ReadFile(string path)
        {
            using (var sr = new StreamReader(path, Encoding.UTF8))
            {
                return sr.ReadToEnd();
            }
        }

        /// <summary>
        /// 获取版本号
        /// </summary>
        /// <returns></returns>
        private static string GetVersion()
        {
            var url = "https://update.leiting.com/gumballs/leiting/ios/dislist/dislist_ios.dis";
            var client = new HttpClient();
            var context = client.Create<string>(HttpMethod.Get, url);
            var result = context.Send().Result;
            var version = result;

            version = version.Substring(version.IndexOf("version"));
            version = version.Remove(version.IndexOf("\n"));
            version = version.Substring(version.IndexOf("-"));
            version = version.Remove(version.IndexOf("\""));
            version = version.Substring(1);
            return version;
        }


        /// <summary>
        /// 获取Manifest文件
        /// </summary>
        /// <param name="version"></param>
        /// <returns></returns>
        private static string GetManifest(string version)
        {
            var url = @"https://gumballsdl.leiting.com/patch/leiting/ios/dislist/leiting_{0}_new/project.manifest";
            var client = new HttpClient();
            var context = client.Create<string>(HttpMethod.Get, string.Format(url, version));
            var result = context.Send().Result;
            return result;
        }


        public static List<Models> GetListForPath(string path)
        {
            var str = ReadFile(path);
            return StringToList(str);
        }

        public static List<Models> GetListForVersion()
        {
            var version = GetVersion();
            var manifest = GetManifest(version);
            return StringToList(manifest);
        }

        private static string Version { get; set; }

        public static void DownPath(string name, string savepath = "")
        {
            if (string.IsNullOrWhiteSpace(Version)) Version = GetVersion();

            var url = @"https://gumballsdl.leiting.com/patch/leiting/ios/dislist/leiting_{0}_new/{1}";
            var client = new HttpClient();
            var context = client.Create<byte[]>(HttpMethod.Get, string.Format(url, Version, name));
            var result = context.Send().Result;

            SaveFile(result, name);
        }


        private static void SaveFile(byte[] file, string name, string savepath = "")
        {
            if (string.IsNullOrWhiteSpace(savepath))
                savepath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            var path = savepath + "Code\\" + name.Replace("/","\\");

            var path1 = path.Remove(path.LastIndexOf("\\"));
            if (!Directory.Exists(path1))
                Directory.CreateDirectory(path1);

            using (var fs = new FileStream(path, FileMode.Create))
            {
                fs.Write(file, 0, file.Length);
            }

        }
    }
}